﻿/**********************************************************************/
/*                                                                    */
/*                   embDocumentInhalator                             */
/*             http://embdocumentinhalator.codeplex.com/              */
/*                                                                    */
/*               (c) Copyright 2010 Ilija Injac         .             */
/*                                                                    */
/*  This source is subject to the CDDL License                        */
/*  http://embdocumentinhalator.codeplex.com/license                  */
/*                                                                    */
/*                                                                    */
/**********************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Text;
using embWIAScan;
using WIA;
using System.IO;


namespace embWIAScan
{
    /// <summary>
    /// Class to perform ADF Multi-Page-Scans
    /// Please rembember that duplex scans are
    /// not possible at this time!
    /// </summary>
    public class embWIAAdfScan
    {
        /// <summary>
        /// Standard constructor.
        /// </summary>
        public embWIAAdfScan()
        {
                
        }
        /// <summary>
        /// Holding the WIA-Device-ID of the Scanner
        /// </summary>
        private string DeviceID;
        private string FileExtension;
        private string ImageFormat;

        /// <summary>
        /// Scan multiple pages through ADF.
        /// </summary>
        /// <param name="wiaImageType">Give the image type here. Use WiaConstants.[ImageFileType]</param>
        /// <param name="filePath">Give the fullpath for the folder to save the images to. Please add a trailing backslash!</param>
        /// <param name="fileName">Give here the basic filename for each image without file extension. The images will be generated like this:filename1.ext filename2.ext ...</param>
        public void ADFScan(string wiaImageType, string filePath, string fileName, object hResolution, object vResolution)
        {
            
            //Choose Scanner
            CommonDialog class1 = new CommonDialog();
            //Show only available scanners in selection dialog
            Device d = class1.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
            if (d != null)
            {
                this.DeviceID = d.DeviceID;

                //Set the filetype for scanning
                switch(wiaImageType)
                {
                    case WIAConstants.wiaFormatBMP:
                        FileExtension = ".bmp";
                        ImageFormat = WIAConstants.wiaFormatBMP;
                    break;
                    case WIAConstants.wiaFormatGIF:
                        FileExtension = ".gif";
                        ImageFormat = WIAConstants.wiaFormatGIF;
                    break;
                    case WIAConstants.wiaFormatJPEG:
                        FileExtension = ".jpeg";
                        ImageFormat = WIAConstants.wiaFormatJPEG;
                    break;
                    case WIAConstants.wiaFormatPNG:
                        FileExtension = ".png";
                        ImageFormat = WIAConstants.wiaFormatPNG;
                    break;
                    case WIAConstants.wiaFormatTIFF:
                        FileExtension = ".tiff";
                        ImageFormat = WIAConstants.wiaFormatTIFF;
                    break;
                    default:
                        FileExtension = ".tiff";
                        ImageFormat = WIAConstants.wiaFormatTIFF;
                    break;

                }


            }
            else
            {
                //no scanner chosen
                return;
            }


            //Create WIA Common dialog
            WIA.CommonDialog WiaCommonDialog = new WIA.CommonDialog();

            bool hasMorePages = true;
            int x = 0;
            int numPages = 0;

            while (hasMorePages)
            {
                //Create DeviceManager
                DeviceManager manager = new DeviceManager();
                Device WiaDev = null;
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.DeviceID == this.DeviceID)
                    {
                        WIA.Properties infoprop = null;
                        infoprop = info.Properties;

                        //connect to scanner
                        WiaDev = info.Connect();


                        break;
                    }
                }



                //Start Scan

                WIA.ImageFile img = null;
                WIA.Item Item = WiaDev.Items[1] as WIA.Item;

                foreach (WIA.Property itemProperty in Item.Properties)
                {
                   

                    if (itemProperty.Name.Equals("Horizontal Resolution"))
                    {

                        ((IProperty)itemProperty).set_Value(ref hResolution);
                    }
                    else if (itemProperty.Name.Equals("Vertical Resolution"))
                    {


                        ((IProperty)itemProperty).set_Value(ref vResolution);
                    }
                }

                try
                {

                    img = (ImageFile)WiaCommonDialog.ShowTransfer(Item, ImageFormat, false);


                    //process image:
                   
                    string varImageFileName = filePath + fileName + x.ToString() + FileExtension ;
                    if (File.Exists(varImageFileName))
                    {
                        //file exists, delete it
                        File.Delete(varImageFileName);
                    }
                    img.SaveFile(varImageFileName);
                    numPages++;
                    img = null;
                    x++;
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("Error: " + ex.Message);
                    //No Pages left
                    hasMorePages = false;
                }
                //finally
                //{
                //    Item = null;
                //    //determine if there are any more pages waiting
                //    Property documentHandlingSelect = null;
                //    Property documentHandlingStatus = null;
                //    foreach (Property prop in WiaDev.Properties)
                //    {
                //        if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                //            documentHandlingSelect = prop;
                //        if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                //            documentHandlingStatus = prop;


                //    }

                //    hasMorePages = false; //assume there are no more pages
                //    if (documentHandlingSelect != null)
                //    //may not exist on flatbed scanner but required for feeder
                //    {
                //        //check for document feeder
                //        if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
                //        {
                //            hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
                //        }
                //    }
                //    x++;
                //}
            } //END WHILE

        }
        
    }
}
